
const http = require('../../utils/http.js');
const app = getApp();
//const utils = require('../../utils/util.js');
let timer;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hide_good_box: true,
    isCansel: false,  //取消搜索文字切换
    hideSeach: true,  //控制取消搜索文字显示隐藏
    historyHide: true, //控制历史记录显示隐藏
    goodsShow: false, //控制商品栏和购物车显示隐藏
    listShow: false,// 控制搜索列表显示隐藏
    goodsArr: [],
    keywords: '',
    historyArr: [],
   
  },
  
  //商品详情
  prodctDetail(e) {
    var gid = e.currentTarget.dataset.id;
    wx.navigateBack({
      success: () => {
        // setTimeout(()=>{
        wx.navigateTo({
          url: '../goodsDetail/goodsDetail?gid=' + gid,
        })
        // },500)
      }
    })

  },
  //商品搜索
  searchFun(e) {
    this.setData({
      historyHide: false,
      listShow: true,
      goodsShow: false,
      keywords: e.detail.value,
      goodsArr: []
    })
    var keyW = e.detail.value;
    if (keyW) {
      this.setData({
        isCansel: true,
        historyHide: false,
      })
    } else {
      //当输入框为空时全部还原
      this.setData({
        isCansel: false,
        hideSeach: true,
        historyHide: true,
        goodsShow: false,
        listShow: false,
      })
      this.getHistory();
    }

    var pages = getCurrentPages();
    //上一个页面实例对象
    var prePage = pages[pages.length - 2];

    if ((this.data.keywords).trim() != '') {
      prePage.getGoodsList(1, 0, '%' + this.data.keywords + '%').then(res => {
        console.log(res)
        this.setData({
          goodsArr: res
        })
        prePage.getGoodsList(1, 0);
      })
    }
  },

  
  searchGoods(e) {
   
   // this.searchFun(e)
   //防抖 防止多次重复请求
    clearTimeout(timer);
    timer = setTimeout(() => {
      this.searchFun(e)
    }, 500);
  },



  //点击搜索按钮
  seachHandele() {
    this.setData({
      hideSeach: false,
      goodsShow: true,
      listShow: false,
    })
  },
  //点击历史记录项
  handleHistory(e) {
    var goods_name = e.currentTarget.dataset.name
    var pages = getCurrentPages();
    //上一个页面实例对象
    var prePage = pages[pages.length - 2];
    this.setData({
      hideSeach: false,
      goodsShow: true,
      listShow: false,
      historyHide: false,
      keywords: goods_name,
      goodsArr: []
    })

    prePage.getGoodsList(1, 0, '%' + goods_name + '%').then(res => {
      console.log(res)
      this.setData({
        goodsArr: res
      })
      prePage.getGoodsList(1, 0);
    })
  },
  //选择商品
  selectGoods(e) {
    var pages = getCurrentPages();
    //上一个页面实例对象
    var prePage = pages[pages.length - 2];

    var goods_name = e.currentTarget.dataset.item.goods_name
    this.setData({
      hideSeach: false,
      goodsShow: true,
      listShow: false,
      goodsArr: [],
      keywords: goods_name,
    })
    prePage.getGoodsList(1, 0, '%' + goods_name + '%').then(res => {
      console.log(res)
      this.setData({
        goodsArr: res
      })
    })
  },

  //点击叉叉
  cancelSearch() {

    this.setData({
      keywords: ''
    })
    if (this.data.keywords) {
      this.setData({
        isCansel: true,
        historyHide: false,
      })
    } else {
      //当输入框为空时全部还原
      this.setData({
        isCansel: false,
        hideSeach: true,
        historyHide: true,
        goodsShow: false,
        listShow: false,
      })
      this.getHistory();
    }
  },

  //跳转购物车
  toShopCar() {
    var pages = getCurrentPages();
    //上一个页面实例对象
    var prePage = pages[pages.length - 2];
    console.log(prePage, '+++');
    if (prePage.route == 'pages/FlashDelivery/FlashDelivery') {
      wx.navigateTo({
        url: '../shopCarDetail/shopCarDetail',
      })
    } else if (prePage.route == 'pages/FlashDelivery1/FlashDelivery1') {
      wx.navigateTo({
        url: '../shopCarDetail1/shopCarDetail1',
      })
    }

  },

  //获取历史记录
  getHistory() {
    var uid = app.globalData.uid;
    http.fetchApi('procedure_t', { p: `R2005007|${uid}` }).then(res => {
      if (res.data.code == 1) {
        var historyArr = res.data.results;
        this.setData({
          historyArr
        })
      }
    })
  },

  //删除历史记录
  deleteHistory() {
    var uid = app.globalData.uid;
    http.fetchApi('procedure_t', { p: `R2005008|${uid}` }).then(res => {
      if (res.data.code == 1) {
        var historyArr = res.data.results;
        this.setData({
          historyArr
        })
      }
    })
  },

  touchOnGoods: function (e) {
    // 如果good_box正在运动
    if (!this.data.hide_good_box) return;
    this.finger = {};
    var topPoint = {};
    this.finger['x'] = e.touches["0"].clientX;
    this.finger['y'] = e.touches["0"].clientY;
    if (this.finger['y'] < this.busPos['y']) {
      topPoint['y'] = this.finger['y'] - 150;
    } else {
      topPoint['y'] = this.busPos['y'] - 150;
    }
    topPoint['x'] = Math.abs(this.finger['x'] - this.busPos['x']) / 2;
    if (this.finger['x'] > this.busPos['x']) {
      topPoint['x'] = (this.finger['x'] - this.busPos['x']) / 2 + this.busPos['x'];
    } else {
      topPoint['x'] = (this.busPos['x'] - this.finger['x']) / 2 + this.finger['x'];
    }
    this.linePos = app.bezier([this.finger, topPoint, this.busPos], 20);
    this.startAnimation();
  },
  startAnimation: function () {
    var index = 0,
      that = this,
      bezier_points = that.linePos['bezier_points'],
      len = bezier_points.length - 1;
    this.setData({
      hide_good_box: false,
      bus_x: that.finger['x'],
      bus_y: that.finger['y']
    })
    this.timer = setInterval(function () {
      index++;
      that.setData({
        bus_x: bezier_points[index]['x'],
        bus_y: bezier_points[index]['y']
      })
      if (index >= len) {
        clearInterval(that.timer);
        that.setData({
          hide_good_box: true,
        })
      }
    }, 15);
  },
  //添加商品到购物车
  addGoods(e) {
    //飞入购物车效果
    this.touchOnGoods(e);
    var pages = getCurrentPages();
    //上一个页面实例对象
    var prePage = pages[pages.length - 2];
    prePage.addNum(e);
    this.setData({
      shopCarNum: prePage.data.shopCarNum
    })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.busPos = {};
    this.busPos['x'] = 20;
    this.busPos['y'] = app.globalData.hh;

    // console.log('购物车坐标', this.busPos, app.globalData.hh)
    this.getHistory();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var pages = getCurrentPages();
    //上一个页面实例对象
    var prePage = pages[pages.length - 2];
    prePage.getGoodsNum();
    this.setData({
      shopCarNum: prePage.data.shopCarNum
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})